package toman.hiber;

import org.apache.derby.jdbc.ClientDataSource40;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import toman.QueryEngineSelectorImpl;
import toman.TOQuery;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: zslajchrt
 * Date: 21.4.11
 * Time: 14:42
 */
public class HMain {
    public static void main(String[] args) throws Exception {
        Map<String, Object> securityContext = new HashMap<String, Object>();
        Map<String, Object> user = new HashMap<String, Object>();
        user.put("name", "Pepa");
        user.put("role", "kuchar");
        securityContext.put("user", user);

        ClientDataSource40 ds = new ClientDataSource40();
        ds.setServerName("localhost");
        ds.setDatabaseName("sun-appserv-samples");
        ds.setUser("APP");
        ds.setPassword("APP");

        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();

        HTOMan toMan = new HTOMan(session);
        ((QueryEngineSelectorImpl)toMan.getQueryEngineSelector()).addEngine(0, new FilterEngine(ds));
        ((QueryEngineSelectorImpl)toMan.getQueryEngineSelector()).addEngine(0, new SecurityContextEngine(securityContext));

        // Get SimplePhotoTO by ID
        SimpleAlbumTO simpleAlbumTO = toMan.findTO(SimpleAlbumTO.class, "a1");
        System.out.println(simpleAlbumTO.getUser().getName());

        // Get SimplePhotoTO by ID
        SimplePhotoTO simplePhotoTO = toMan.findTO(SimplePhotoTO.class, 351);

        // Get list of SimplePhotoTO by HQL query
        List<SimplePhotoTO> simplePhotoTOList1 = toMan.createQuery(SimplePhotoTO.class, "album.name='a1'").getResults();

        // Get list of SimplePhotoTO by a proprietary filter
        PhotoFilter filter = new PhotoFilter();
        Calendar cal = Calendar.getInstance();
        cal.set(2011, 3, 22);
        filter.setCreated(new java.sql.Date(cal.getTimeInMillis()));
        List<SimplePhotoTO> simplePhotoTOList2 = toMan.createQuery(SimplePhotoTO.class, filter).getResults();


        BasicAlbumWithPhotosTO basicAlbumWithPhotosTO = toMan.findTO(BasicAlbumWithPhotosTO.class, "a1");
        System.out.println(basicAlbumWithPhotosTO);

        BasicPhotoAlbumWithDateTO albumWithPhotosTO = toMan.findTO(BasicPhotoAlbumWithDateTO.class, 351);
        for (BasicPhotoAlbumTO to : albumWithPhotosTO.getSameDatePhotos()) {
            to.setPhotoDescription(to.getPhotoDescription() + "*");
        }

        Transaction tx = session.beginTransaction();

        toMan.updateTO(albumWithPhotosTO);

        tx.commit();


        // List all photos
        TOQuery<BasicPhotoAlbumTO> query = toMan.createQuery(BasicPhotoAlbumTO.class, null);
        List<BasicPhotoAlbumTO> results = query.getResults();
        for (BasicPhotoAlbumTO to : results) {
            System.out.println(to.getPhotoId() + " " + to.getPhotoDescription() + " " + to.getAlbumName());
        }

        tx = session.beginTransaction();

        // finding the photo for a given ID
        BasicPhotoAlbumTO to = toMan.findTO(BasicPhotoAlbumTO.class, 351);
        to.setPhotoDescription(to.getPhotoDescription() + "!");
        // update the photo's description
        toMan.updateTO(to);

        tx.commit();

    }
}
